﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using FileManage.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using FM.Api.ViewModels;
using System.Text.RegularExpressions;
using System.Collections.Concurrent;
using Microsoft.Extensions.Options;
using FM.Storage.Abstract;
using FM.Storage.Models;

namespace FM.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FileV2Controller : ControllerBase
    {

        private readonly ILocalFileManager _localFileManager;

        public FileV2Controller(ILocalFileManager localFileManager)
        {
            _localFileManager = localFileManager;
        }

        [Route("upload")]
        [HttpPost]
        public async Task<ActionResult<UploadResult>> UploadFile()
        {
            var request = Request;

            HttpContext.Request.Query.TryGetValue("chunkNumber", out var chunkNumbers);
            int.TryParse(chunkNumbers.ToString(), out int chunkNumber);
            //httpContext.Request.Query.TryGetValue("identifier", out var identifiers);
            //if (chunkNumber == 0 || string.IsNullOrEmpty(identifiers))
            //{
            //    return new UploadResult()
            //    {
            //        msg = "上传为文件为空"
            //    };
            //}
            //else
            //{
            //    var chunkFilename = _localFileManager.GetChunkFilename(chunkNumber, identifiers);
            //    if (!System.IO.File.Exists(chunkFilename))
            //    {
            //        return new UploadResult()
            //        {
            //            msg = "文件名不存在"
            //        };
            //    }
            //}
            if (chunkNumber > 1)//chunkUpload
            {
                var result = await _localFileManager.ChunkUploadAsync(request);
                return result;
            }
            else
            {
                var result = await _localFileManager.UploadAsyncAsync(request);
                return result;
            }

        }
    }
}